﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Chart
{
    public partial class DayAmpChartFrm : System.Windows.Forms.Form
    {
        public DayAmpChartFrm()
        {
            InitializeComponent();
        }

        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void InitTimer()
        {
            timer.Interval = 2000;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }

        // 여기서 차트를 갱신해 준다.
        private void OnTimerTick(object sender, EventArgs e)
        {
            DownloadFutOptDailyData();
            // 차트에 데이터를 대응시켜 준다.
            SetChartData();
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (m_CPDataSet == null)
                return;
            //m_CPDataSet.MakeDailyAmp();
            SetChartData();
        }

        // Cybos Plus Data Set Member
        private ETDataSet m_CPDataSet = null;
        public EasyTrader.ETDataSet CPDataSet
        {
            get { return m_CPDataSet; }
            set { m_CPDataSet = value; }
        }
        
        private void InitControl()
        {
            FutureDailyAmpChartCtrl.Series["고가"].ToolTip = "#VALX : #VAL";
            FutureDailyAmpChartCtrl.Series["저가"].ToolTip = "#VALX : #VAL";
        }
        
        private void SetChartData()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null || GlobalVar.FutOptDailyDataDownloaded == false)
                return;

            FutureDailyAmpChartCtrl.Series["고가"].Points.Clear();
            FutureDailyAmpChartCtrl.Series["저가"].Points.Clear();
            EasyTrader.DataSet.ETDataTable dataTable = m_CPDataSet.GetFutOptDailyTable();
            if (dataTable == null)
                return;

            int rowCount = dataTable.GetRowCount();
            double high = 0;
            double low = 0;
            for (int i = 0; i < rowCount; i++)
            {
                DataRow row = dataTable.Rows[i];
                try
                {
                    // 값이 없을 경우는 넘어 간다.
                    if (row["고가"] == DBNull.Value)
                        continue;

                    high = Convert.ToDouble(row["고가"]);
                    low = Convert.ToDouble(row["저가"]);
                    double height = high - low;
                    FutureDailyAmpChartCtrl.Series["고가"].Points.AddXY(i, height / 2);
                    string date = row["날짜"].ToString();
                    FutureDailyAmpChartCtrl.Series["고가"].Points[i].AxisLabel = date.Substring(4);
                    FutureDailyAmpChartCtrl.Series["저가"].Points.AddXY(i, -(height / 2));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }

        public DayAmpChartFrm(ETDataSet a_DataSet)
        {
            InitializeComponent();
            m_CPDataSet = a_DataSet;
            InitControl();
            InitTimer();
        }

        private void InitChart()
        {
            FutureDailyAmpChartCtrl.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(223)))), ((int)(((byte)(193)))));
            FutureDailyAmpChartCtrl.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            FutureDailyAmpChartCtrl.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(181)))), ((int)(((byte)(64)))), ((int)(((byte)(1)))));
            FutureDailyAmpChartCtrl.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            FutureDailyAmpChartCtrl.BorderlineWidth = 2;
            FutureDailyAmpChartCtrl.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.Interval = 1;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.Interval = 2;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.Interval = 1;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.MinorGrid.Interval = 0.5;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.Interval = 1;

            FutureDailyAmpChartCtrl.ChartAreas["Price"].Area3DStyle.IsClustered = true;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].Area3DStyle.Perspective = 10;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].Area3DStyle.IsRightAngleAxes = false;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].Area3DStyle.WallWidth = 0;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].Area3DStyle.Inclination = 15;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].Area3DStyle.Rotation = 10;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.IsLabelAutoFit = true;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = false;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.IsLabelAutoFit = true;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.IsStartedFromZero = true;
            //FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.LabelAutoFitMaxFontSize;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            FutureDailyAmpChartCtrl.ChartAreas["Price"].BackSecondaryColor = System.Drawing.Color.White;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            FutureDailyAmpChartCtrl.ChartAreas["Price"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].Position.Auto = true;
            //FutureDailyAmpChartCtrl.ChartAreas["Price"].Position.Height = 80F;
            //FutureDailyAmpChartCtrl.ChartAreas["Price"].Position.Width = 80F;
            //FutureDailyAmpChartCtrl.ChartAreas["Price"].Position.X = 5F;
            //FutureDailyAmpChartCtrl.ChartAreas["Price"].Position.Y = 10F;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].ShadowColor = System.Drawing.Color.Transparent;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = true;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisX.IsMarginVisible = true;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.IsEndLabelVisible = true;
            FutureDailyAmpChartCtrl.ChartAreas["Price"].AxisY.IsMarginVisible = true;
            //FutureDailyAmpChartCtrl.Series[0].Color = Color.Red;
            //DataPointCustomProperties aa = FutureDailyAmpChartCtrl.Series[0]["PriceUpColor"];
        }

        private void DayAmpChartFrm_Load(object sender, EventArgs e)
        {
            InitChart();
            m_CPDataSet.MakeCmeDailyAmp();
            SetChartData();
        }

        private void DayAmpChartFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
        }

        

        private void buttonCmeLoad_Click(object sender, EventArgs e)
        {
        }

        private void DownloadFutOptDailyData()
        {
           
        }
        private void buttonDayLoad_Click(object sender, EventArgs e)
        {
           
        }
    }
}
